<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/static/xadmin/css/font.css">
    <link rel="stylesheet" href="/static/xadmin/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/apublic.css">
    <script src="/static/xadmin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/xadmin/js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="/static/xadmin/html5.min.js"></script>
    <script src="/static/xadmin/respond.min.js"></script>
    <![endif]-->
    <script src="/static/until.js" charset="utf-8"></script>
    <script src="/static/vue.js" charset="utf-8"></script>
    <script>
        if(!sessionStorage.getItem('token')) window.top.location.href='/login.html';
    </script>
    <style>
        body{
            background:#fff;
        }
        [v-cloak] {
            display: none !important;
        }
        .layui-icon-close{
            cursor:pointer;
        }
        .layui-icon-close:hover{
            color:red;
        }
        .attr>div>div:nth-child(2){
            padding-left:50px;
        }
        .attr>div>div:nth-child(3){
            padding-left:50px;
        }
        .item-attr{
            padding:3px 0px;
        }
        .item-attr-title{
            padding:2px 0px;
        }
        .item-val{
            padding:2px 0px;
        }
        .delattr,.delval,.addval{
            margin-left:20px;
            cursor: pointer;
        }
        .addvalinput{
            background:#eee;
            border:0px;
        }
        input{padding:0px 3px;}
        table td{
            padding:2px!important;
        }
        table input{
            width:50px!important;
        }
        .title{
            background:#eee;
            font-size:16px;
            font-weight: bold;
            padding:5px;
            display:flex;
            justify-content: space-between;
            align-items: center;
        }
        .addattr{
            cursor: pointer;
            padding:5px 5px;
            display:block;
            width:100px;
        }
    </style>
</head>
<body>
<div v-cloak class="layui-fluid" id="app">
    <div class="layui-row">
        <div class="title"><span>属性</span><span v-on:click="save_attr()" class="layui-btn layui-btn-small">保存</span></div>
        <div class="attr">
            <div><span v-on:click="addattr" class="layui-btn layui-btn-small addattr">添加属性</span></div>
            <div v-for="(item,index) in goods_attr" class="item-attr">
                <div class="item-attr-title"><input :value="item.goods_attribute.title" v-model="item.goods_attribute.title"><span class="delattr" v-on:click="delattr(index)">删除</span></div>
                <div>
                    <div v-for="(it,ind) in item.goods_attribute.goods_attribute_value" class="item-val"><input :value="it.title" v-model="it.title"><span class="delval" v-on:click="delval(index,ind)">删除</span></div>
                </div>
                <div><input readonly class="addvalinput" value="+"/><span class="addval" v-on:click="addval(index)">添加</span></div>
            </div>
            
        </div>
    </div>
</div>
<script>
    layui.use(['layer','jquery'],
        function() {
            $ = layui.jquery;
            var app = new Vue({
                el: '#app',
                data: {
                    goods_attr:[]
                },
                created:function(){
                },
                methods:{
                    save_attr(){
                        var that=this
                        var ids=getQueryString("ids").split(",").filter(function (s) {return s && s.trim();});
                        var attr_arr=[]
                        ids.forEach(element => {
                            that.goods_attr.forEach(item=>{
                                var tem=JSON.parse(JSON.stringify(item))
                                tem.goods_id=Number(element)
                                attr_arr.push(tem)
                            })
                        });
                        u_post("/goods/add_attr",JSON.stringify(attr_arr),function(res){
                            if(res.status.errorCode==0){
                                that.goods_attr=[]
                                layer.msg("完成")
                            }else{
                                layer.alert(res.status.msg, {icon: 5})
                            }
                        })
                    },
                    addattr(){
                            this.goods_attr.push({
                                                        "goods_id": 0,
                                                        "attribute_id": 0,
                                                        "goods_attribute": {
                                                            "id": 0,
                                                            "title": "",
                                                            "goods_attribute_value": [{
                                                                "id": 0,
                                                                "attribute_id": 0,
                                                                "title": ""
                                                            }]
                                                        }
                                                    })
                    },
                    addval(index){
                        this.goods_attr[index].goods_attribute.goods_attribute_value.push(
                                                                {
                                                                    "id": 0,
                                                                    "attribute_id": 0,
                                                                    "title": ""
                                                                })
                    },
                    delattr(index){
                        
                        this.goods_attr.splice(index,1)
                    },
                    delval(index,ind){
                        this.goods_attr[index].goods_attribute.goods_attribute_value.splice(ind,1)
                    },
                }
            }) 

        })
 
    
</script>
</body>

</html>
